package com.monitor.zabbix.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.monitor.zabbix.dao.TriggerDao;
import com.thinkgem.jeesite.common.persistence.Page;

@Service
public class ZabbixApiService {
    @Value("${zabbix.apiUrl}")
    private String apiUrl;
    @Value("${zabbix.username}")
    private String username;
    @Value("${zabbix.password}")
    private String password;
    @Value("${zabbix.auth}")
    private String auth;

    @Autowired
    private TriggerDao triggerDao;

    public Map<String, Object> getHost(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        if (!params.containsKey("output")) {
            params.put("output", "extend");
        }
        map = ZabbixApiUtil.request(apiUrl, "host.get", params, auth);
        return map;
    }

    public Map<String, Object> getTemplate(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        params.put("output", "extend");
        map = ZabbixApiUtil.request(apiUrl, "template.get", params, auth);
        return map;
    }



    public Map<String, Object> createHost(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        params.put("output", "extend");

        Map<String, Object> templateMap = new HashMap<>();
        getTemplate(templateMap);

        map = ZabbixApiUtil.request(apiUrl, "host.create", params, auth);
        return map;
    }

    public Map<String, Object> getHistoryData(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        params.put("history", 0);
        if (!params.containsKey("output")) {
            params.put("output", "extend");
        }
        if (!params.containsKey("sortfield")) {
            params.put("sortfield", "clock");
        }
        if (!params.containsKey("sortorder")) {
            params.put("sortorder", "DESC");
        }
        if (!params.containsKey("time_from")) {
            params.put("time_from", System.currentTimeMillis() - 60 * 60 * 24 * 30 * 1000);
        }

        if (!params.containsKey("time_till")) {
            params.put("time_till", System.currentTimeMillis());
        }


        map = ZabbixApiUtil.request(apiUrl, "history.get", params, auth);
        return map;
    }

    public Map<String, Object> getAlert(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        params.put("output", "extend");
        map = ZabbixApiUtil.request(apiUrl, "alert.get", params, auth);
        return map;
    }

    public Map<String, Object> login(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        Object usernameObj = params.get("username");
        if (usernameObj == null || "".equals(usernameObj)) {
            params.put("user", username);
        }
        Object passwordObj = params.get("password");
        if (passwordObj == null || "".equals(passwordObj)) {
            params.put("password", password);
        }
        map = ZabbixApiUtil.request(apiUrl, "user.login", params, null);
        return map;
    }


    public Map<String, Object> getItem(Map<String, Object> map) throws Exception {
        JSONObject params = new JSONObject();
        params.putAll(map);
        if (!params.containsKey("output")) {
            params.put("output", "extend");
        }


        map = ZabbixApiUtil.request(apiUrl, "item.get", params, auth);
        return map;
    }



    /**
     * 查询问题
     * 
     * @param params
     * @return
     */
    public List<Map<String, Object>> findProblem(Map<String, Object> params) {

        List<Map<String, Object>> problems = triggerDao.findProblem(params);
        List<String> ids = new ArrayList<>();
        for (Map<String, Object> thisMap : problems) {
            ids.add(thisMap.get("triggerid").toString());
        }

        Map<String, Object> triggerParams = new HashMap<>();
        triggerParams.put("ids", ids);
        List<Map<String, Object>> hostList = findHostByTrigger(triggerParams);

        for (Map<String, Object> thisMap : problems) {
            String triggerId = thisMap.get("triggerid").toString();
            for (Map<String, Object> hostMap : hostList) {
                String hostTriggerId = hostMap.get("triggerid").toString();
                if (triggerId.equals(hostTriggerId)) {
                    thisMap.putAll(hostMap);
                }
            }
        }
        return problems;
    }

    public List<Map<String, Object>> findHostByTrigger(Map<String, Object> params) {
        List<Map<String, Object>> returnList = new ArrayList<Map<String,Object>>();
        if(params.containsKey("ids")){
            @SuppressWarnings("unchecked")
            List<String> idList = (List<String>) params.get("ids");
            if(!idList.isEmpty()){
                returnList = triggerDao.findHostByTrigger(params);
            }
        }
        return returnList;
    }



    /**
     * 查询问题
     * 
     * @param params
     * @return
     */
    public Page<Map<String, Object>> findHistoryData(Page<Map<String, Object>> page,
            Map<String, Object> params) {
        // 设置分页参数
        params.put("page", page);
        // 执行分页查询
        page.setList(triggerDao.findHistoryDataByMap(params));
        return page;
    }

    /**
     * 根据条件查询host
     * 
     * @param params
     * @return
     */
    public List<Map<String, Object>> findHostByMap(Map<String, Object> params) {
        return triggerDao.findHostByMap(params);
    }

}
